
package com.jsfcompref.model;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIData;
import javax.faces.component.html.HtmlSelectBooleanCheckbox;
import java.util.List;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.imageio.ImageIO;
import javax.sql.rowset.serial.SerialBlob;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import webservices.Mensaje;

import webservices.Servicio;
import webservices.Proveedor;

@ManagedBean
@RequestScoped
public class ServicioBacking {
    
    private UIData servs;
    private UIData provs;
     
    private StreamedContent image;

    public void setImage(StreamedContent image) {
        this.image = image;
    }
    
    
    
    public UIData getServs() {
        return servs;
    }

    public void setServs(UIData servicios) {
        this.servs = servicios;
    }

    public UIData getProvs() {
        return provs;
    }

    public void setProvs(UIData provs) {
        this.provs = provs;
    }
    
    public String contactar()
    {
      Servicio serv = (Servicio) getServs().getRowData();
      Proveedor prov = (Proveedor) getProvs().getRowData();
             
      FacesContext context = FacesContext.getCurrentInstance();
      ELContext elContext = context.getELContext();
      Application application = context.getApplication();
            
      ExpressionFactory expressionFactory = application.getExpressionFactory();
      ValueExpression ve = expressionFactory.createValueExpression(elContext, "#{mensajeBean}",MensajeBean.class);
      MensajeBean mensajeBean = (MensajeBean) ve.getValue(elContext);
      
      mensajeBean.setProv(prov);
      mensajeBean.setServ(serv);
         
      Mensaje ms = new Mensaje();
      mensajeBean.setMensaje(ms);
      
     return "contactar";
    }
    
    public StreamedContent getImage() throws SQLException, IOException {
     
        File fil = new File("c:\\tv19.jpg");
        BufferedImage originalImage = ImageIO.read(fil);
        
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(originalImage,"jpg",baos);
               
        //serv.getListaImagenes().add(baos.toByteArray());
        Blob bild = new SerialBlob(baos.toByteArray());
       // Blob bild = new SerialBlob(pBlob);
        InputStream stream = bild.getBinaryStream();
        image = new DefaultStreamedContent(stream, "image/jpg");
     return image;
    }
    
    public String contratarServicio()
    {
        
      Servicio serv = (Servicio) getServs().getRowData();
      Proveedor prov = (Proveedor) getProvs().getRowData();
      
      FacesContext context = FacesContext.getCurrentInstance();
      ELContext elContext = context.getELContext();
      Application application = context.getApplication();
            
      ExpressionFactory expressionFactory = application.getExpressionFactory();
      ValueExpression ve = expressionFactory.createValueExpression(elContext, "#{uBean}",UBean.class);
      UBean ubean = (UBean) ve.getValue(elContext);
      
      ubean.setServic(serv);
      ubean.setProv(prov);
      
      String resp = "contrato";
      
      if ("webservices.HotelServicio".equals(serv.getClass().getName()))
      {
          resp="contrato";
      }
      
      if ("webservices.TourServicio".equals(serv.getClass().getName()))
      {
          resp="contratotour";
      }
      if ("webservices.AutoServicio".equals(serv.getClass().getName()))
      {
          resp="contratoauto";
      }
      
      if ("webservices.TransporteServicio".equals(serv.getClass().getName()))
      {
          resp="contratotransporte";
      }
      return resp;
      
    }
}
 